package com.sc.student.controller;

import com.sc.student.pojo.Course;
import com.sc.student.pojo.CourseType;
import com.sc.student.pojo.Student;
import com.sc.student.service.CourseService;
import com.sc.student.service.CourseTypeService;
import com.sc.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @program: abdcefg
 * 描述：
 * @author: NB
 * @create: 2020-09-27 19:18
 **/
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    CourseService courseService;
    @Autowired
    CourseTypeService courseTypeService;

    @RequestMapping("login")
    public String login(String username, String password, Map map) {
        Student student = studentService.selectByUsernameAndPasswrod(username, password);

        if (student == null) {
            return "";
        }
        map.put("user", student);

        return "redirect:/student/toShow";
    }

    @RequestMapping("/toShow")
    public String toShow(Integer courseTypeId, Map map) {
        if (courseTypeId == null) {
            courseTypeId = 1;
        }
        List<Course> courses = courseService.selectByCourseType(courseTypeId);
        List<CourseType> courseTypes = courseTypeService.selectAll();
        map.put("courseType", courseTypes);
        map.put("course", courses);
        return "forward:/home.jsp";
    }

    @RequestMapping("/toCourse")
    public String toCourse(String id, Map map) {
        Course course = courseService.selectById(Integer.parseInt(id));
        map.put("course", course);
        return "/course";
    }
}
